home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / I-Z / XLisp_1.6.cpt / MacFun2.MEM < prev    next >
Text File  |  1985-12-04  |  5KB  |  126 lines

  1. Macintosh Toolbox Interface Functions
  2.     November 3, 1985
  3.  
  4. Here are some functions that have been added to XLISP in the version 1.5b
  5. release.  They are specific to the Macintosh version and are intended to
  6. provide a low level interface to the Macintosh toolbox.  They are VERY
  7. dangerous!!  Any incorrect use of these function could easily result in
  8. a system crash and/or disk file corruption.  Use these functions carefully
  9. and make sure you backup your files before trying anything.
  10.  
  11. (toolbox <trap> [<arg>]...)      Call a toolbox procedure (no return value)
  12. (toolbox-16 <trap> [<arg>]...)   Call a toolbox function with a 16 bit value
  13. (toolbox-32 <trap> [<arg>]...)   Call a toolbox function with a 32 bit value
  14.  
  15. All of these functions make it possible to call the stack-based toolbox
  16. routines.  All of the arguments are integers (FIXNUMs) and they are all
  17. interpreted as 16 bit values.  If you need to pass a 32 bit address, you'll
  18. have to take it apart and pass it as two separate 16 bit arguments.  Pass
  19. only as many arguments as the toolbox routine expects.  Additional arguments
  20. won't be ignored.  They will cause a system bomb!
  21.  
  22. The first argument of each of these functions is the 16 bit trap word used
  23. by assembly language programmers to call the toolbox routine.  You can
  24. specify it in hex if you use the reader facility for entering hex numbers:
  25.  
  26.     #xA954 will read as the decimal number 43348
  27.     #x100  will read as the decimal number 256
  28.  
  29. The remaining arguments are pushed onto the stack before the trap word
  30. is executed.  They are all treated as 16 bit integers.
  31.  
  32. The value returned will be the value left on the stack by the toolbox
  33. routine.  Any toolbox routines that return values in reference parameters
  34. will need to be handled by passing the address of a place to store the
  35. result.  You can do this by some fancy manipulation of the ADDRESS-OF
  36. function.  Be careful.
  37.  
  38. (NewHandle <size>)  Allocate a relocatable block of memory
  39. (NewPtr <size>)  Allocate a non-relocatable block of memory
  40.  
  41. These functions allocate memory.  The first returns a handle to the space
  42. allocated and the second returns a pointer to the space.  In both cases,
  43. the size is an integer.  The result is also an integer whose value is the
  44. 32 bit address of the handle or allocated space.
  45.  
  46. (HiWord <num>)  Return the high order 16 bits of an integer
  47. (LoWord <num>)  Return the low order 16 bits of an integer
  48.  
  49. These functions simplify separating a 32 bit value into two 16 bit halves.
  50. They are useful where it is necessary to pass 32 bit pointers or handles
  51. to the toolbox interface functions.
  52.  
  53. *command-window*   Address of the command window (FIXNUM)
  54. *graphics-window*  Address of the graphics window (FIXNUM)
  55.  
  56. These global variables contain the WindowPtrs to the command and graphics
  57. windows in the standard XLISP user interface.
  58.  
  59. These functions will eventually become part of the generic version of
  60. XLISP.
  61.  
  62. (peek <adr>)  Peek at a value in memory
  63. (poke <adr> <val>)  Poke a value into memory
  64.  
  65. In both cases, the address is an integer (FIXNUM).  The number of bits
  66. that are significant in the address depends on the host processor.  For
  67. the 68000 (the processor in the Macintosh), FIXNUMs are 32 bits and so
  68. are addresses, so all bits are significant.  The value returned by
  69. PEEK and accepted by POKE is also an integer.  The number of significant
  70. bits is determined by the size of an 'int' in the underlying  C
  71. implementation.  For the Macintosh, 16 bits are significant.
  72.  
  73. (address-of <expr>)  Get the machine address of an XLISP node
  74.  
  75. This function returns the location in memory of the specified XLISP value.
  76. It returns an integer (FIXNUM) whose value is the address of the node used
  77. internally to represent the value.
  78.  
  79. (read-char-no-hang)  Read a character from the keyboard without waiting
  80.  
  81. This function checks if a character is available from the keyboard.  If
  82. a character is available, READ-CHAR-NO-HANG returns the character without
  83. echoing it.  The character is returned as a integer whose value is the
  84. ASCII code of the character.  If no character is available, the function
  85. returns NIL.
  86.  
  87. If you need to call any of the register based toolbox routines, you will
  88. have to resort to something like the following example.  Here we are
  89. building an array containing machine code and then fooling XLISP into
  90. thinking that it is a built-in function.  This is also VERY dangerous!
  91. Use with caution!
  92.  
  93. ; (make-subr <code-list>)  Make a SUBR node
  94.  
  95. (defun make-subr (code &aux subr hunk adrs)
  96.  
  97.   ; allocate some space for the code
  98.   (setq hunk (NewPtr (* (length code) 2)))
  99.  
  100.   ; stuff the code into the space
  101.   (dotimes (i (length code))
  102.     (poke (+ hunk i i) (car code))
  103.     (setq code (cdr code)))
  104.  
  105.   ; create a node we can convert to a SUBR
  106.   (setq subr (+ 1 1))
  107.   (setq adrs (address-of subr))
  108.  
  109.   ; initialize the node type (SUBR = 1)
  110.   (poke adrs #x100)
  111.  
  112.   ; store the address of the code space
  113.   (poke (+ adrs 2) (HiWord hunk))
  114.   (poke (+ adrs 4) (LoWord hunk))
  115.  
  116.   ; return the new SUBR node
  117.   subr)
  118.  
  119. ; try it out with the SysBeep(10) test
  120. (setq beep (make-subr '(
  121.   #x3F3C #x000A  ; move.w #10,-(SP)
  122.   #xA9C8         ; _SysBeep
  123.   #x4280         ; clr.l D0 ; return NIL to XLISP
  124.   #x4E75)))      ; rts
  125.  
  126.